[Previous] [Next]

Chapter 10

Windows Common Controls: Part I

Apart from the intrinsic controls described in Chapter 3, the Microsoft Windows common controls are probably the most widely used controls in Microsoft Visual Basic applications. The main reason for their popularity is that they contribute to creating the Windows look-and-feel more than any other group of controls. For example, a Windows Explorer-like application with a TreeView control on the left and a ListView control on the right immediately reminds users of Windows Explorer itself, thus making for a familiar interface.

Windows common controls were first introduced in Windows 95. The initial group of these controls included the TreeView, ListView, ImageList, Toolbar, StatusBar, TabStrip, and Slider controls. Visual Basic 4—the first 32-bit version of this language—offered an OLE Custom Control (OCX) that provided access to their features. After this initial release, Microsoft manufactured several new common controls as well as more powerful versions of the original ones. But the OCXs that came with Visual Basic 5 (and its three service packs) were never significantly updated, which meant that Visual Basic programmers couldn't use these newer controls in their applications (with the exceptions of the new Animation and UpDown controls). Until recently, to use such newer controls or to fully exploit the capabilities of the original controls, Visual Basic developers had to resort to third-party Microsoft ActiveX controls or to complex API programming.

Visual Basic 6 has remedied this problem. It now includes all the tools you need to fully take advantage of the features of nearly all existing common controls. A few Windows common controls are still missing from the newer versions of the OCX files (most notably, the IP Address control), but in most cases you won't need to purchase any additional custom controls to give your Visual Basic applications a modern user interface.

Most of the Windows common controls come in the MsComCtl.ocx file. This file contains all the original common controls, plus the ImageCombo control. Another file, MsComCt2.ocx, includes the code for five additional controls: Animation, UpDown, MonthView, DateTimePicker, and FlatScrollBar. I describe these controls in Chapter 11. Note that these two files correspond to the outdated files ComCtl32.ocx and ComCt232.ocx that were distributed with Visual Basic 5. Visual Basic 6 doesn't replace the older OCX files and can peacefully coexist with Visual Basic 5 and applications developed with that version of the language.

Visual Basic 6 also includes a third OCX file, ComCt332.ocx, which adds support for yet another common control, the CoolBar control (also described in Chapter 11). This file has been available to Visual Basic 5 developers, who had to download it from the Microsoft web site, but the file is now included in the Visual Basic package.

OCX files that are distributed with Visual Basic 6 have an interesting feature. While the older ComCtl32.ocx and ComCt232.ocx files were just intermediaries between Visual Basic and the system DLLs that included the actual code for the controls, the newer versions are self-contained and include all the necessary code for manipulating the controls. Needless to say, newer files are bigger than their predecessors—MsComCtl.ocx is more than 1 MB—but this approach simplifies the distribution of Visual Basic applications and reduces the chance of conflicts with programs already installed on the end user's machine.

In this chapter, I describe the features of all the Windows common controls included in MsComCtr.ocx, while the next chapter covers all the controls in MsComCt2.ocx and ComCt232.ocx. To test the code presented here, you first make the controls available to your Visual Basic environment. To do so, execute the Components command from the Project menu (or just press the Ctrl+T key combination) to display the Components dialog box similar to the one in Figure 10-1, and then select the OCXs that interest you, as shown in Figure 10-2.

When you load into the Visual Basic 6 environment a project built with a previous version of the language that contains references to older versions of Windows common controls, a message box appears asking whether you want to upgrade those controls with their newer versions. For the maximum compatibility with existing applications, you might decide to continue to use the older controls, even though upgrading to the newer versions is the best choice if you want to improve the application's user interface and functionality. You can turn this warning off by deselecting the Upgrade ActiveX Controls check box on the General tab of the Project Properties dialog box.

Click to view at full size.

Figure 10-1. The Components dialog box, with all the Windows common controls OCXs selected.

Figure 10-2. The common controls in the Toolbox.